/**
 * @author scylla
 * @version V1.0
 * @Project echo-java-base
 * @Title
 * @Description *
 * @Package concurrent.mthread.callable
 * @Since 2021/7/27 10:10 上午
 */
package concurrent.mthread.callable;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.*;

public class CallableMain {
    public static void main(String[] args) {
        ExecutorService exec = Executors.newCachedThreadPool();
        ArrayList<Future<String>> results = new ArrayList<>();

        for (int i = 0; i < 10; i++)
            results.add(exec.submit(new TaskWithResult(i)));

        for (Future<String> fs : results) {
            try {
                while (!fs.isDone())
                    System.out.println(MessageFormat.format("{0} 任务还未完成!", Thread.currentThread().getName()));
                while (Objects.isNull(fs.get(100, TimeUnit.MILLISECONDS)) )
                    System.out.println(MessageFormat.format("{0} 获取不到结果@", Thread.currentThread().getName()));
                System.out.println(MessageFormat.format("{0} 执行完毕，结果：{1}", Thread.currentThread().getName(), fs.get()));
            } catch (InterruptedException | ExecutionException | TimeoutException e) {
                e.printStackTrace();
            } finally {
                exec.shutdown();
            }
        }
    }
}
